export default {
    data() {
        return {
            appId: process.env.VUE_APP_APPID,
            debug: true,
            timestamp: parseInt(new Date().getTime() / 1000),
            nonceStr: 'Wm3WZYPz0wzccnW',
            locationHref: `${process.env.VUE_APP_URL}/wx`
        }
    },

    methods: {

        //设置产品利率
        setRateName(type, interestRate, interestRateEnd) {
            let string = ['', '日', '月', '年'];

            //return interestRateEnd ? `区间利率${interestRate} - ${interestRateEnd}` : `${string[type]}利率${interestRate}`;
            return `${string[type]}利率${interestRate}`;
        },

        //
        renderTagType(type) {
            switch (type) {
                case 0:
                    return "";
                case 1:
                    return "热门";
                case 2:
                    return "推荐";
                case 3:
                    return "赞";
            }
        },

        //隐藏手机号中间4位数
        hidePhone(tel) {
            return tel.length ? tel.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2") : '';

        },


        //发送验证码
        async sendPhone(phone) {
            return this.$http.get(`/mp/user/sendSms`, {params: {phone}});
        },


        showImg(url) {
            return process.env.VUE_APP_BASE_URL+url;
        },


        async getCompany(keyword, page) {
            return await this.$http.get(`common/mp/qcc/company/listPage?keyword=${keyword}&page=${page}&pageSize=10`);
        },

        async getCompanyInfo(keyword) {
            return await this.$http.get(`/common/mp/qcc/company?keyword=${keyword}`);
        },

        wxIsReady() {
            return new Promise((resolve, reject) => {
                wx.ready(_ => {
                    //微信初始化成功
                    resolve(true);
                });

                wx.error(_ => {
                    //微信初始化失败
                    resolve(false);
                })
            })
        },


        //   微信选择图片方法    type === 'album', 'camera'
        async chooseImage(type) {
            let isReady = this.wxIsReady();
            if (!isReady) return this.$toast.error('微信初始化失败');
            return new Promise((resolve, reject) => {
                wx.chooseImage({
                    count: 1, // 默认9
                    sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
                    sourceType: [type], // 可以指定来源是相册还是相机，默认二者都有
                    success(res) {
                        wx.getLocalImgData({
                            localId: res.localIds[0], // 图片的localID
                            success: function (res) {
                                resolve(res.localData);
                            },
                            failed(){
                                reject();
                            }
                        });


                    },
                    cancel() {
                        reject();
                    }
                });
            });

        }
    }
}
